/*
	Copyright (c) 2004, 2006 Pablo Bleyer Kocik.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	3. The name of the author may not be used to endorse or promote products
	derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
	WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
	MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
	EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
	BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
	IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

import java.util.Vector;
import java.util.Date;

/** PicoBlaze-2 assembler. */
public class Assembler2 extends Assembler {

	/** Instruction factory. */
	private static Instruction2 insn;
	// 1<<10 program size, 3 char address, 5 char data
	public static int
		PROGRAM_SIZE = 1<<10,
		ADDRESS_CHARS = 3,
		CODE_CHARS = 5; // 18/4 nibbles

	public Assembler2() {
		super(PROGRAM_SIZE, ADDRESS_CHARS, CODE_CHARS);
		if (insn == null) insn = new Instruction2();
	}

	/** Create new assembler object and process expressions.
		@param v vector of assembly expressions
	*/
	public Assembler2(Vector<AsmExpression> v) {
		super(PROGRAM_SIZE, ADDRESS_CHARS, CODE_CHARS);
		if (insn == null) insn = new Instruction2();

		parse(v);
		assemble(v);
	}

	public int code(String n, Vector a) {
		return insn.code(n, a, environment);
	}

	public String toBlockRAM(String name) {
		StringBuffer b = new StringBuffer();

		b.append(
"/*\n" +
" * " + name + " generated by KCAsm at " + (new Date()).toString() + "\n" +
" */\n\n" +

"module " + name + "(address, instruction, clk);\n"
		);
		b.append(
"input [9:0] address;\n" +
"output [17:0] instruction;\n" +
"input clk;\n" +
"RAMB16_S18 ram_1024_x_18(\n" +
"	.DI (16'b0),\n" +
"	.DIP (2'b0),\n" +
"	.EN (1'b1),\n" +
"	.WE (1'b0),\n" +
"	.SSR (1'b0),\n" +
"	.CLK (clk),\n" +
"	.ADDR (address),\n" +
"	.DO (instruction[15:0]),\n" +
"	.DOP (instruction[17:16])\n" +
");\n" +
"// synthesis translate_off\n"
		);

		String[] hn = new String[0x40+0x08];

		// INIT
		for (int i=0; i<0x40; ++i) {
			String h = "";
			for (int j=0; j<16; ++j) h = String.format("%04X", program[j+i*16] & 0xffff) + h;
			hn[i] = h;
		}
		// INITP
		for (int i=0; i<0x08; ++i) {
			String h = "";
			for (int j=0; j<64; ++j)
				h = String.format("%X", (program[i*128+j*2+1]>>14) & 0xc | (program[i*128+j*2]>>16) & 0x3 ) + h;
			hn[0x40+i] = h;
		}

		b.append(
"/*\tsynthesis\n"
		);
		for (int i=0; i<0x40; ++i)
			b.append(
"\tinit_" +
	String.format("%02X", i) + " = \"" + hn[i] + "\"\n"
			);
		for (int i=0; i<0x08; ++i)
			b.append(
"\tinitp_" +
	String.format("%02X", i) + " = \"" + hn[0x40+i] + "\"\n"
			);
		b.append(
"*/\n"
		);

		b.append(
"// synthesis translate_off\n"
		);

		for (int i=0; i<0x40; ++i)
			b.append(
"defparam ram_1024_x_18.INIT_" +
	String.format("%02X", i) + " = 256'h" + hn[i] + ";\n"
			);
		for (int i=0; i<0x08; ++i)
			b.append(
"defparam ram_1024_x_18.INITP_" +
	String.format("%02X", i) + " = 256'h" + hn[0x40+i] + ";\n"
			);

		b.append(
"// synthesis translate_on\n"
		);

		for (int i=0; i<0x40; ++i)
			b.append(
"// synthesis attribute INIT_" +
	String.format("%02X", i) + " of ram_1024_x_18 is \"" +
	hn[i] + "\"\n"
		);
		for (int i=0; i<0x08; ++i)
			b.append(
"// synthesis attribute INITP_" +
	String.format("%02X", i) + " of ram_1024_x_18 is \"" +
	hn[0x40+i] + "\"\n"
		);

		b.append(
"endmodule\n"
		);

		return b.toString();
	}
}
